﻿namespace Pp.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using Core;
    using Core.Extentions;
    using Domain.Model;
    using Dto;
    using Globalization;
    using NHibernate;
    using NHibernate.Criterion;

    public class ProjectCategoryRepository : BaseRepository<ProjectCategory>
    {
        public ProjectCategoryRepository(ISession session, HttpServerUtilityBase server)
            : base(session, server)
        {
        }

        public virtual IList<ProjectCategoryDto> GetActives()
        {
            var categories = GetAll().ToDto();
            return categories;
        }

        public Result Create(string name, string description, string image, Guid? parentId)
        {
            var parent = Get(parentId);
            var exist = TotalCount(BaseCriteria.Add(Restrictions.Eq("Name", name)));
            if (exist > 0) return Result.AsError(Res.Information_There_is_a_category);
            var cat = new ProjectCategory
                          {
                              Name = name,
                              Description = description,
                              Parent = parent
                          };
            Save(cat);
            return Result.AsSuccess();
        }

        public string GetNameById(Guid? guid)
        {
            return BaseCriteria.Add(Restrictions.Eq("Id", guid)).SetProjection(Projections.GroupProperty("Name")).UniqueResult
                <string>();
        }
    }
}